package com.example.dialog_box;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_dialog1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSimpleDialog();
            }
        });


        findViewById(R.id.btn_dialog2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDeleterDialog();
            }
        });

        findViewById(R.id.btn_dialog3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDropdownListDialog();
            }
        });

        findViewById(R.id.btn_dialog4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showRadiodownListDialog();
            }
        });

        findViewById(R.id.btn_dialog5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCheckboxdownListDialog();
            }
        });
    }

    /**
     * 弹出窗口1
     */
    private void showSimpleDialog() {
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("提示")
                .setMessage("学习中")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .create()
                .show();
        ;
    }

    /**
     * 弹出窗口2
     */
    private void showDeleterDialog() {
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("弹出警告窗口")
                .setMessage("确定要删除吗")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,
                                "positive:" + which,
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,
                                "positive:" + which,
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "positive:" + i,
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
        ;
    }

    /**
     * 下拉列表
     */
    private void showDropdownListDialog() {
        final String[] cities = {"广州", "上海", "北京", "香港", "澳门"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("选择一个城市")
                .setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "选择的城市为:" + cities[i],
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    /**
     * 下拉单选框
     */
    private void showRadiodownListDialog() {
        final String[] sex = {"男", "女", "未知性别"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle(" 请选择性别")
                .setSingleChoiceItems(sex, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "性别为:" + sex[i],
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .show();
    }

    /**
     * 下拉多选框
     */
    private void showCheckboxdownListDialog() {
        final String[] hobbies={"篮球","足球","听音乐","看电影","健身"};
        final boolean orChecked[]=new boolean[5];
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("您的个人爱好")
                .setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        orChecked[which]=isChecked;

                        if (isChecked) {
                            Toast.makeText(MainActivity.this, "您添加了"+hobbies[which], Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(MainActivity.this, "您取消了"+hobbies[which], Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        StringBuffer buffer=new StringBuffer();
                        for (int j=0;j<orChecked.length;j++){
                            if(orChecked[j]){
                                buffer.append(hobbies[j]);
                            }
                        }
                        Toast.makeText(MainActivity.this,"您的爱好有："+buffer,Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

}
